jconed.toolbar = {};

jconed.toolbar._handlers = {};

jconed.toolbar.init = function ()
{
	var tb = jconed.toolbar;

	//tb.buttons = {};
	tb.hidden = true;
	tb.fullscreen = false;

	tb._check_id = null;

	tb.container = document.createElement ( 'div' );
	tb.container.className = 'jconed-toolbar';
	
	tb.handle = document.createElement ( 'div' );
	tb.handle.className = 'jconed-handle';
	tb.handle.innerHTML = 'JConEd alpha';
	
	tb.context = document.createElement ( 'div' );
	tb.context.className = 'jconed-context';

	tb.holder = document.createElement ( 'div' );
	tb.holder.className = 'jconed-holder';

	tb.command = document.createElement ( 'div' );
	tb.command.className = 'jconed-command';

	// events
	liwe.events.add ( tb.handle, 'mousedown', function ( ev )
	{
		tb.drag_start ( ev );
	} );

	liwe.events.add ( tb.handle, 'mouseup', function ( ev )
	{
		tb.drag_stop ( ev );
	} );

	jconed.config.toolbar.iterate ( function ( group )
	{
		if ( group === '-' )
		{
			tb.add_clear ( tb.holder );
			return;
		}
		group.iterate ( function ( button )
		{
			tb.add_button ( button, tb.holder );
		} );
		tb.add_separator ( tb.holder );
	} );
	tb.add_clear ( tb.holder );

	tb.system_register ();

	tb.add_button ( 'Confirm', tb.command );
	tb.add_button ( 'Cancel', tb.command );
	tb.add_checkbox ( 'EditLines', 'Editing lines', tb.command );

	tb.container.appendChild ( tb.handle );
	tb.container.appendChild ( tb.holder );
	tb.container.appendChild ( tb.context );
	tb.container.appendChild ( tb.command );

	document.body.appendChild ( tb.container );

	tb.hide ();

	tb.container.style.left = liwe.dom.get_offset_left ( tb.container ) + 'px';
	tb.container.style.top = liwe.dom.get_offset_top ( tb.container ) + 'px';

	tb.ready = true;
};

jconed.toolbar.set_context = function ( html )
{
	jconed.toolbar.context.innerHTML = html;
};

/*
jconed.toolbar.resize_context = function ()
{
	jconed.toolbar.container.style.height = 'auto';
};
*/

jconed.toolbar.add_button = function ( id, container, extra_cls )
{
	var button = document.createElement ( 'div' );
	button.id = 'jconed-button-' + id;
	button.className = 'jconed-button ' + ( extra_cls ? extra_cls + ' ' : '' ) + button.id;
	button.onclick = function () { jconed.toolbar.execute ( id ); };
	button.title = id;
	//jconed.toolbar.buttons [ id ] = button;
	container.appendChild ( button );
	return button;
};

jconed.toolbar.add_checkbox = function ( id, text, container )
{
	var check = document.createElement ( 'input' );
	check.type = 'checkbox';
	check.id = id;
	check.onchange = function () { jconed.toolbar.execute ( id ); };
	var label = document.createElement ( 'label' );
	label.setAttribute ( 'for', id );
	label.innerHTML = text;
	container.appendChild ( check );
	container.appendChild ( label );
};

jconed.toolbar.add_separator = function ( container )
{
	var sep = document.createElement ( 'div' );
	sep.className = 'jconed-separator';
	container.appendChild ( sep );
};

jconed.toolbar.add_clear = function ( container )
{
	var ender = document.createElement ( 'div' );
	ender.className = 'jconed-clear';
	container.appendChild ( ender );
};

jconed.toolbar.execute = function ( id )
{
	if ( jconed.toolbar._handlers [ id ] )
	{
		if ( ! jconed.editor.current ) return;
		jconed.toolbar._handlers [ id ] ( jconed.editor.current );
		if ( jconed.editor.current )
			$ ( jconed.editor.current.id ).focus ();
	}
	else
		console.warn ( 'Handler not found for: %s', id );
};

jconed.toolbar.register_handler = function ( id, cback )
{
	jconed.toolbar._handlers [ id ] = cback;
};

jconed.toolbar.show = function ( x, y )
{
	//moved to position
	if ( x && y )
	{
		jconed.toolbar.container.style.left = x + 'px';
		jconed.toolbar.container.style.top = y + 'px';
	}
	jconed.toolbar.hidden = false;
	liwe.dom.show ( jconed.toolbar.container );
};

jconed.toolbar.position = function ( x, y )
{
	jconed.toolbar.container.style.left = x + 'px';
	jconed.toolbar.container.style.top = y + 'px';
}

jconed.toolbar.hide = function ()
{
	jconed.toolbar.hidden = true;
	liwe.dom.hide ( jconed.toolbar.container );
};

jconed.toolbar.drag_start = function ( ev )
{
	ev.preventDefault ();
	
	var tb = jconed.toolbar;

	tb.is_dragging = true;

	tb.origX = parseInt ( tb.container.style.left, 10 );
	tb.origY = parseInt ( tb.container.style.top, 10 );

	jconed.toolbar.startX = ev.clientX;
	jconed.toolbar.startY = ev.clientY;
};

jconed.toolbar.drag_stop = function ( ev )
{
	ev.preventDefault ();
	jconed.toolbar.is_dragging = false;
};

jconed.toolbar.move_to = function ( x, y )
{
	var tb = jconed.toolbar;
	tb.container.style.left = x + 'px';
	tb.container.style.top = y + 'px';
};

jconed.toolbar.drag_move = function ( ev )
{
	var tb = jconed.toolbar;
	if ( ! tb.is_dragging ) return;
	ev.preventDefault ();
	var x = tb.origX - ( tb.startX - ev.clientX ); 
	var y = tb.origY - ( tb.startY - ev.clientY ); 
	if ( tb.fullscreen )
	{
		var sizes = tb.get_sizes ();
		var btn_h = sizes [ 0 ], cmd_h = sizes [ 1 ], tot_h = btn_h + cmd_h;
		if ( y < tot_h + 10 )
			y = tot_h + 10;
	}
	tb.move_to ( x, y );
};

jconed.toolbar.get_position = function ()
{
	var tb = jconed.toolbar;
	return [
		parseInt ( tb.container.style.left, 10 ),
		parseInt ( tb.container.style.top, 10 )
	];
}

jconed.toolbar.get_sizes = function ()
{
	var tb = jconed.toolbar;
	var btn_h = liwe.dom.get_size ( tb.holder ) [ 1 ];
	var cmd_h = liwe.dom.get_size ( tb.command ) [ 1 ];
	return [ btn_h, cmd_h ];
}

jconed.toolbar.toggle_fullscreen = function ( b )
{
	var tb = jconed.toolbar;
	if ( tb.fullscreen )
	{
		clearInterval ( tb._check_id );
		liwe.dom.del_class ( document.body, 'jconed-fullscreen' );
		document.body.style.marginTop = '';
		tb.command.style.top = '';
	}
	else
	{
		liwe.dom.add_class ( document.body, 'jconed-fullscreen' );
		tb._check_id = setInterval ( function ()
		{
			var sizes = tb.get_sizes ();
			var btn_h = sizes [ 0 ], cmd_h = sizes [ 1 ], tot_h = btn_h + cmd_h;
			document.body.style.marginTop = tot_h + 'px';
			tb.command.style.top = btn_h + 'px';
			var pos = tb.get_position ();
			if ( pos [ 1 ] < tot_h + 10 )
				tb.move_to ( pos [ 0 ], tot_h + 10 );

		}, 200 );
	}
	tb.fullscreen = ! tb.fullscreen;
};

jconed.toolbar.system_register = function ()
{
	if ( typeof system == 'undefined' ) return;

	var mods = system.admin.get_modules ();
	var acts;
	//var btns = [];
	var id;
	var tb = jconed.toolbar;

	mods.iterate ( function ( mod )
	{
		if ( ! ( acts = mod.get_plugin_actions () ) ) return;

		acts.iterate ( function ( act, pos )
		{
			id = 'plugin_' + mod.module_name.replace ( '.', '_' ) + '_' + pos;
			//tb.register_handler ( id, function () { act [ 'handler' ] ( id ) } );
			tb.register_handler ( id, act [ 'handler' ] );
			//btns.push ( id );
			var btn = tb.add_button ( id, tb.holder, 'jconed-button-plugin' );
			btn.title = act [ 'label' ];
			//btn.style.backgroundImage = 'url(' + act [ 'img_src' ] + ')';
			jconed.register_context_handler ( act [ 'context' ] );
		} );

	} );

	tb.add_clear ( tb.holder );

	//jconed.config.toolbar.push ( btns );
};

jconed.toolbar.register_handler ( 'Confirm', function ( editor )
{
	editor.detach ();
	if ( editor.cback [ 'confirm' ] )
		editor.cback [ 'confirm' ] ( editor );
} );

jconed.toolbar.register_handler ( 'Cancel', function ( editor )
{
	editor.detach ();
	if ( editor.cback [ 'cancel' ] )
		editor.cback [ 'cancel' ] ( editor );
	$( editor.id, editor._html );
} );

jconed.toolbar.register_handler ( 'EditLines', function ( editor )
{
	var check = $ ( 'EditLines' ).checked;
	if ( check )
		liwe.dom.add_class ( $( editor.id ), 'jconed-edit-mode' );
	else
		liwe.dom.del_class ( $( editor.id ), 'jconed-edit-mode' );
} );


liwe.events.add ( document, 'mousemove', jconed.toolbar.drag_move );
